/**
 * UsersController
 *
 * @description :: Server-side logic for managing users
 * @help        :: See http://sailsjs.org/#!/documentation/concepts/Controllers
 */
User = require('../models/User');
var LoginBean = require('../jsbean/LoginBean');
module.exports = {



  /**
   * `UsersController.zhuce()`
   */
  zhuce: function (req, res) {
    //推荐的入库操作
    // console.log(req.body);
    var user=req.allParams();
    User.create(user).exec(function (err,created) {

      res.send('注册成功');
    })
    // User.create().exec(function (err,created){
    //   console.log(err);
    //   console.log(created);     //返回的是创建的对象
    // })

    // User.query('insert into user set name=?,pwd=?',['gg','gg'],function(err,results){
    //   console.log('错误:'+err);
    //   console.log(results);
    // })

  },


  /**
   * `UsersController.login()`
   */
  login: function (req, res) {

    if(req.body==undefined){
      res.view();
    }else{
      User.query('select id,name from user where name=? and pwd=?',[req.body['name'],req.body['pwd']],function(err,results){
        // console.log('错误:'+err);
        // console.log(results);
        if(results.length>0){
          loginbean=new LoginBean();
          loginbean.id=results[0].id;
          loginbean.name=results[0].name;
          req.session.loginbean=loginbean;
          // res.send('denglu');
          res.redirect('/test');
        }else{
          res.send("用户密码错误")
        }
      })


    }

  },
  test:function (req,res) {
    console.log(111111);
    loginbean=req.session.loginbean;
    console.log(loginbean);
    res.view('/users/test',{loginbean:loginbean});

  }
};

